﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WebDemo.Filters;

namespace WebDemo
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services
            config.Filters.Add(new ValidateModelAttribute());
            config.Filters.Add(new LogHandleErrorAttribute());
            //config.Filters.Add(new AuthorizeAttribute());

            var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
            config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);//去掉xml的支持，只提供json

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            //// Controller Only
            //// To handle routes like `/api/VTRouting`
            //config.Routes.MapHttpRoute(
            //    name: "ControllerOnly",
            //    routeTemplate: "api/{controller}"
            //);

            //// Controller with ID
            //// To handle routes like `/api/VTRouting/1`
            //config.Routes.MapHttpRoute(
            //    name: "ControllerAndId",
            //    routeTemplate: "api/{controller}/{id}",
            //    defaults: null,
            //    constraints: new { id = @"^\d+$" } // Only integers 
            //);

            //// Controllers with Actions
            //// To handle routes like `/api/VTRouting/route`
            //config.Routes.MapHttpRoute(
            //    name: "ControllerAndAction",
            //    routeTemplate: "api/{controller}/{action}"
            //);
        }
    }
}
